package br.com.persist.core;

import br.com.persist.api.PersistentContext;
import br.com.persist.api.RuntimeExecutionException;
import br.com.persist.api.VolatileContext;

/**
 * Utility methods for running compiled resources ({@link PersistResource}).
 */
public class PersistRunner {

	public Object run(PersistResource resource, VolatileContext volatileContext, PersistentContext persistentContext)
			throws RuntimeExecutionException {
		return resource.run(volatileContext, persistentContext);
	}

	public Object run(PersistResource resource, PersistentContext persistentContext) throws RuntimeExecutionException {
		VolatileContext volatileContext = new DefaultVolatileContext();
		try {
			return resource.run(volatileContext, persistentContext);
		} finally {
			volatileContext.releaseAll();
		}
	}

	public Object run(FunctionPersistResource resource, VolatileContext volatileContext, PersistentContext persistentContext, Object... parameters)
			throws RuntimeExecutionException {
		return resource.runWithParameters(volatileContext, persistentContext, parameters);
	}

	public Object run(FunctionPersistResource resource, PersistentContext persistentContext, Object... parameters) throws RuntimeExecutionException {
		VolatileContext volatileContext = new DefaultVolatileContext();
		try {
			return resource.runWithParameters(volatileContext, persistentContext, parameters);
		} finally {
			volatileContext.releaseAll();
		}
	}
}
